package com.choudou5.solr.service;

import cn.hutool.core.collection.CollUtil;
import com.choudou5.solr.annotation.ServiceInfo;
import com.choudou5.solr.bean.SysDynamicBean;
import com.choudou5.solr.framework.constants.UserConsts;
import com.choudou5.solr.util.cache.CacheFactory;
import com.choudou5.solr.util.cache.impl.LFUCache;

import java.util.List;

/**
 * @name 系统动态 接口
 * @author choudou5
 * @date 2018-7-13
 */
@ServiceInfo(table = "solr_sys_dynamic", desc = "系统动态")
public class SysDynamicService extends BaseService<SysDynamicBean> {

    private static final LFUCache<String, List<SysDynamicBean>> CACHE_TOP = CacheFactory.newLFUCache(UserConsts.MAX_ONLINE, 60 * 1000); //60秒有效期

    private static class SingletonHolder{
        private static SysDynamicService instance = new SysDynamicService();
    }

    /**
     * 获取单例
     * @return
     */
    public static SysDynamicService getInstance() {
        return SingletonHolder.instance;
    }

    public List<SysDynamicBean> findTop(Integer userId, int top){
        String cacheKey = userId+"_"+top;
        List<SysDynamicBean> list = CACHE_TOP.get(cacheKey);
        if(CollUtil.isEmpty(list)){
            list = findList("create_by = "+ userId+" ORDER BY create_time DESC LIMIT " + top);
            CACHE_TOP.put(cacheKey, list);
        }
        return list;
    }


}
